// pages/dynrelese/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgslist:[],
    title:'',
    textarea:'',
    avatar:'',
    images:[],//选择图片
    images_success: [],//上传云存储后的云存储地址数组
    images_success_size:0,//图片上传成功的数
  },
  upload(){
    let that=this;
    wx.chooseImage({//异步方法
      count: 9,//最多选择图片数量
      sizeType:['original', 'compressed'],//选择的图片尺寸 原图，压缩图
      sourceType:['album','camera'],//相册选图，相机拍照
      success(res){
        //const tempFilePaths = res.tempFilePaths
        that.setData({
          images: res.tempFilePaths
         });
         console.log("选择成功",res)
      }
    })
  },
  uploadImage(index){
    let that=this
      wx.cloud.uploadFile({//上传至微信云存储
        cloudPath:'myImage/' + new Date().getTime() + "_" +  Math.floor(Math.random()*1000) + ".jpg",//使用时间戳加随机数给图片命名
        filePath:that.data.images[index],// 本地文件路径
        success: res => {
          // 返回文件 ID
          console.log("上传成功",res.fileID)
          that.data.images_success[index] = res.fileID;
          that.data.images_success_size = that.data.images_success_size+1;
 
          if(that.data.images_success_size == that.data.images.length){
            console.log("上传成功：", that.data.images_success)
          } else {
            that.uploadImage(index+1)
          }
        },
        fail: err =>{
          wx.showToast({
            icon:'none',
            title: '上传失败，请重新上传',
          })
        }
      })
  },
  handleTitle(e){
      console.log(e.detail.value)
      this.setData({
          title:e.detail.value
      })
  },
  handleText(e){
      this.setData({
          textarea:e.detail.value
      })
  },
  addDynamicitem(){
    let that=this;
    if(that.data.images.length > 0){//1、判断是否有图片
      that.setData({
        //3、给上传图片初始化一个长度，上传成功的数组和已有的数组一致
        images_success:that.data.images
      })
      that.uploadImage(0)//2、有图片时先上传第一张
    }
    wx.cloud.callFunction({
        name:'adddynamicitem',
        data:{
            avatar:wx.getStorageSync('userinfo').avatarUrl,
            imgs:that.data.images_success,
            itemid:Math.floor(Math.random() * 10000),
            love_num:0,
            name:wx.getStorageSync('userinfo').nickName,
            title:that.data.title,
            textarea:that.data.textarea
        },
        success:res=>{
            console.log(res)
            that.setData({
                images:'',
                title:'',
                textarea:''
            })
            wx.showToast({
              title: '发布成功',
              duration:1000
            })
        },
        fail:err=>{
            console.log(err)
        }
    })
    that.handleText('')
    that.handleTitle('')
  },

  
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
  this.setData({
      avatar:wx.getStorageSync('userinfo').avatarUrl
  })
  console.log(this.data.avatar)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})